package DAO;

import java.sql.ResultSet;
import java.util.ArrayList;

import POJO.KhuyenMaiSP;
import Util.DataProvider;

public class KhuyenMaiSPDAO
{
    public static ArrayList<KhuyenMaiSP> SelectKhuyenMaiSPALL()
    {
        ArrayList<KhuyenMaiSP> listKhuyenMai = new ArrayList<KhuyenMaiSP>();

        String sql = "SELECT * FROM KhuyenMaiSP";

        try
        {
            DataProvider dp = new DataProvider();
            dp.open();

            ResultSet rs = dp.ExecuteQuery(sql);
            while (rs.next())
            {
                KhuyenMaiSP kmsp = new KhuyenMaiSP();

                kmsp.setMaKhuyenMai(rs.getInt("maKhuyenMai"));
                kmsp.setMaSanPham(rs.getInt("maSanPham"));
                kmsp.setPhanTramGiam(rs.getInt("phanTramGiam"));
                kmsp.setNgayBatDau(rs.getString("ngayBatDau"));
                kmsp.setNgayKetThuc(rs.getString("ngayKetThuc"));
                kmsp.setTinhTrang(rs.getNString("tinhTrang"));

                listKhuyenMai.add(kmsp);
            }

            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
        return listKhuyenMai;
    }

    public static KhuyenMaiSP SelectKhuyenMaiSPById(KhuyenMaiSP khuyenMaiSPID)
    {
        KhuyenMaiSP kmsp = new KhuyenMaiSP();

        try
        {
            String sql = "SELECT * FROM KHUYENMAISP WHERE MAKHUYENMAI="
                    + khuyenMaiSPID;
            DataProvider dp = new DataProvider();
            dp.open();

            ResultSet rs = dp.ExecuteQuery(sql);
            while (rs.next())
            {
                kmsp.setMaKhuyenMai(rs.getInt("maKhuyenMai"));
                kmsp.setMaSanPham(rs.getInt("maSanPham"));
                kmsp.setPhanTramGiam(rs.getInt("phanTramGiam"));
                kmsp.setNgayBatDau(rs.getString("ngayBatDau"));
                kmsp.setNgayKetThuc(rs.getString("ngayKetThuc"));
                kmsp.setTinhTrang(rs.getNString("tinhTrang"));
            }

            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }

        return kmsp;
    }

    public static boolean InsertKhuyenMaiSP(KhuyenMaiSP kmsp)
    {
        boolean kq = false;

        String sql = String
                .format("INSERT INTO KHUYENMAISP(MASANPHAM, PHANTRAMGIAM, NGAYBATDAU, NGAYKETTHUC, TINHTRANG) VALUES(%d, %d, '%s', '%s', N'%s')",
                        kmsp.getMaSanPham(), kmsp.getPhanTramGiam(),
                        kmsp.getNgayBatDau(), kmsp.getNgayKetThuc(),
                        kmsp.getTinhTrang());

        try
        {
            DataProvider dp = new DataProvider();
            dp.open();
            int i = dp.executeUpdate(sql);
            if (i == 1)
                kq = true;
            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }

        return kq;
    }

    public static boolean UpdateKhuyenMaiSP(KhuyenMaiSP kmsp)
    {
        boolean kq = false;

        String sql = String
                .format("UPDATE KHUYENMAISP SET MASANPHAM=%d, PHANTRAMGIAM=%d, NGAYBATDAU='%s', NGAYKETTHUC='%s', TINHTRANG=N'%s' WHERE MAKHUYENMAI='%s'",
                        kmsp.getMaSanPham(), kmsp.getPhanTramGiam(),
                        kmsp.getNgayBatDau(), kmsp.getNgayKetThuc(),
                        kmsp.getTinhTrang());

        try
        {
            DataProvider dp = new DataProvider();
            dp.open();
            int i = dp.executeUpdate(sql);
            if (i == 1)
                kq = true;
            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }

        return kq;
    }

    public static boolean DeleteKhuyenMaiSP(String KhuyenMaiSPID)
    {
        boolean kq = false;

        String sql = "DELETE FORM KHUYENMAISP WHERE MASANPHAM="
                + KhuyenMaiSPID;

        try
        {
            DataProvider dp = new DataProvider();
            dp.open();
            int i = dp.executeUpdate(sql);
            if (i == 1)
                kq = true;
            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }

        return kq;
    }
}
